package com.zy.core.web;

import com.zy.core.web.entity.JsonResult;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Package: core.web
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-9-8 10:45
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
public class BaseController {

    /**
     * 处理表单提交日期问题
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 成功
     * @return
     */
    public JsonResult success(){
        return new JsonResult().success();
    }

    /**
     * 成功
     * @param code 状态码
     * @return
     */
    public JsonResult success(Integer code){
        return new JsonResult().success(code);
    }

    /**
     * 成功
     * @param msg 描述
     * @return
     */
    public JsonResult success(String msg){
        return new JsonResult().success(msg);
    }

    /**
     * 成功
     * @param code 状态码
     * @param msg 描述
     * @return
     */
    public JsonResult success(Integer code, String msg){
        return new JsonResult().success(code, msg);
    }

    /**
     * 错误
     * @return
     */
    public JsonResult error(){
        return new JsonResult().error();
    }

    /**
     * 错误
     * @param code 状态码
     * @return
     */
    public JsonResult error(Integer code){
        return new JsonResult().error(code);
    }

    /**
     * 错误
     * @param msg 状态描述
     * @return
     */
    public JsonResult error(String msg){
        return new JsonResult().error(msg);
    }

    /**
     * 错误
     * @param code 错误码
     * @param msg 错误描述
     * @return
     */
    public JsonResult error(Integer code, String msg){
        return new JsonResult().error(code,msg);
    }

    public JsonResult failure(){
        return new JsonResult().failure();
    }

    /**
     * 失败
     * @param code 状态码
     * @return
     */
    public JsonResult failure(Integer code){
        return new JsonResult().failure(code);
    }

    /**
     * 是啊比
     * @param msg 状态描述
     * @return
     */
    public JsonResult failure(String msg){
        return new JsonResult().failure(msg);
    }
    /**
     * 失败
     * @param code 状态码
     * @param msg 状态描述
     * @return
     */
    public JsonResult failure(Integer code, String msg){
        return new JsonResult().failure(code,msg);
    }
}
